# pip install matplotlib  numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
import matplotlib.pyplot as plt
import numpy as np
from pylab import mpl

mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False


# 创建画布
fig, ax = plt.subplots(figsize=(10, 6))

# 绘制堤坝
dam_height = 6  # 堤坝高度6米
dam_width = 50  # 堤坝宽度50米
ax.bar(0, dam_height, width=dam_width, color='saddlebrown', label='堤坝')

# 绘制绿化林带
forest_start = dam_width  # 林带起始位置
forest_width = 100  # 林带宽度100米
tree_spacing = 4  # 树木间距4米
tree_height = 2.5  # 树木高度2.5米

# 生成树木的位置
x_trees = np.arange(forest_start, forest_start + forest_width, tree_spacing)
y_trees = np.full_like(x_trees, dam_height)  # 树木在堤坝顶部

# 绘制树木
ax.scatter(x_trees, y_trees, s=100, c='darkgreen', marker='^', label='白皮松')

# 绘制农田
field_start = forest_start + forest_width  # 农田起始位置
field_width = 150  # 农田宽度150米
ax.bar(field_start, 1, width=field_width, color='limegreen', label='农田')

# 设置坐标轴
ax.set_xlim(-10, field_start + field_width + 10)
ax.set_ylim(0, dam_height + 5)
ax.set_xlabel('距离 (米)')
ax.set_ylabel('高度 (米)')
ax.set_title('堤坝、绿化林带与农田效果图')

# 添加图例
ax.legend()

# 显示图像
plt.grid(True, linestyle='--', alpha=0.6)
plt.show()